package com.zzl.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.crud.bean.Employee;
import com.zzl.crud.bean.Msg;
import com.zzl.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;


    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Msg deleteEmp(@PathVariable("ids")String ids){
        if(ids.contains("-")){
            List<Integer> listId = new ArrayList();
            String[] strIds = ids.split("-");
            for (String id : strIds) {
                listId.add(Integer.parseInt(id));
            }
            employeeService.delEmpAll(listId);
        }else {
            Integer id = Integer.parseInt(ids);
            employeeService.delEmp(id);
        }
        return Msg.success();
    }
    /**
     * 员工添加
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmp(Employee employee){
        System.out.println(employee);
        employeeService.updateEmp(employee);
        return Msg.success();
    }
    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public Msg getEmp(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmp(id);
        return Msg.success().add("emp",employee);
    }
    /**
     * 获取员工列表
     * @param pn
     * @param model
     * @return
     */
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){

        PageHelper.startPage(pn,5);
        List<Employee> emps = employeeService.getAll();
        PageInfo pageInfo = new PageInfo(emps,5);

        model.addAttribute(pageInfo);
        return "list";
    }

    /**
     * 查询所有员工
     * 返回json格式的数据
     */
    @ResponseBody
    @RequestMapping("emps")
    public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1") Integer pn){

        PageHelper.startPage(pn,5);
        List<Employee> emps = employeeService.getAll();
        PageInfo pageInfo = new PageInfo(emps,5);

        return Msg.success().add("pageInfo",pageInfo);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Employee employee, BindingResult result){
        Map<String,Object> map = new HashMap<>();
        if (result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String name = fieldError.getField();
                String message = fieldError.getDefaultMessage();
                map.put(name,message);
            }
            return Msg.failure().add("errorFields",map);
        }else {
            employeeService.saveEmp(employee);
            return Msg.success();
        }

    }

    @ResponseBody
    @RequestMapping("/checkuser")
    public Msg checkuser(String empName){
        String regx = "(^[a-zA-Z0-9_-]{6,15}$)|(^[\\u4e00-\\u9fa5]{2,5})";
        boolean matches = empName.matches(regx);
        if (!matches){
            return Msg.failure().add("va_msg","用户名是中文2到5位或者字母6到15位组合");
        }
        boolean b = employeeService.checkUser(empName);
        if (b){
            return Msg.success().add("va_msg","用户名可用");
        }
        return Msg.failure();
    }
}
